﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Enquiries.Referrals {
    public partial class Create : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "id")) {
                return;
            }

            EnquiryIdCaption.Text = Request.QueryString["id"];
            EnquiryIdCaption.NavigateUrl = "~/Enquiries/View.aspx?id=" + Request.QueryString["id"];
            GetUsers();
        }

        private void GetUsers() {
            var users = UserRepository.QueryOver().Where(u => u.IsActive).OrderBy(u => u.DisplayName).Asc.List();

            Referees.Items.Clear();
            Referees.Items.Add(new ListItem
            {
                Value = "0",
                Text = "--Select--"
            });
            foreach (var user in users) {
                var userItem = new ListItem
                {
                    Value = user.Id.ToString(CultureInfo.InvariantCulture),
                    Text = user.DisplayName
                };

                if (user.FirstPointOfContact) {
                    userItem.Text = user.DisplayName + " (Front Line for " + user.Department.DepartmentName + ")";
                }

                Referees.Items.Add(userItem);
            }
            //BindDropDownList(Referees, users, "DisplayName", "Id", "--Select--", "0");
        }

        protected void ReferEnquiryClick(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, false, "id")) {
                return;
            }

            var selectedUserId = int.Parse(Referees.SelectedItem.Value);
            var referee = UserRepository.GetById(selectedUserId);

            if (referee == null) {
                ReferEnquiryResultMessage.Text = "Please select a user!";
                return;
            }

            var enquiryId = int.Parse(Request.QueryString["id"]);
            var enquiry = EnquiryRepository.GetById(enquiryId);
            var viewUrl = ConfigSettings.SiteRoot + "Enquiries/View.aspx?id=" + enquiryId;

            var referral = enquiry.Refer(LoggedUser, referee, viewUrl);

            if (!IsValidObject(referral, ReferEnquiryResultMessage)) {
                return;
            }

            var currentReferral = new EnquiryCurrentReferral
                {
                    CurrentlyReferredTo = referral.ReferredTo,
                    CurrentlyReferredToDepartment = referral.ReferredToDepartment,
                    CurrentlyReferredToGroup = referral.ReferredToGroup
                };

            EnquiryCurrentReferralRepository.Add(currentReferral);
            enquiry.CurrentReferral = currentReferral;

            EnquiryRepository.Update(enquiry);

            if (referee.Department.Id != enquiry.Category.Department.Id) {
                //needs to be recategorised
                Response.Redirect("~/Enquiries/Referrals/CreateNewDepartment.aspx?id=" + enquiry.Id + "&dept=" +
                                  referee.Department.Id);
            }
            else {
                Response.Redirect("~/Enquiries/View.aspx?id=" + enquiry.Id);
            }
        }
    }
}